package cn.edu.ncu.housinghub.service.impl;

import cn.edu.ncu.housinghub.constant.ErrorCodeConstant;
import cn.edu.ncu.housinghub.dto.request.contract.ContractRequest;
import cn.edu.ncu.housinghub.dto.request.contract.ContractVerifyRequest;
import cn.edu.ncu.housinghub.entity.contract.Contract;
import cn.edu.ncu.housinghub.entity.contract.ContractApplication;
import cn.edu.ncu.housinghub.entity.house.House;
import cn.edu.ncu.housinghub.entity.user.Agent;
import cn.edu.ncu.housinghub.entity.user.Landlord;
import cn.edu.ncu.housinghub.entity.user.User;
import cn.edu.ncu.housinghub.exception.BusinessException;
import cn.edu.ncu.housinghub.mapper.ContractMapper;
import cn.edu.ncu.housinghub.mapper.HouseMapper;
import cn.edu.ncu.housinghub.mapper.UserMapper;
import java.util.Date;
import cn.edu.ncu.housinghub.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private UserMapper userMapper;



//    @Autowired
//    private OSSClient ossClient;

    /**
     * 租客发起签约申请
     */
    @Override
    @Transactional
    public void addInitContract(ContractApplication application) {
        Long houseId = application.getHouseId();
        Long landlordId = application.getLandlordId();
        // 1. 验证参数
        if (houseId == null||landlordId == null) {
            throw new BusinessException(ErrorCodeConstant.PARAM_VALIDATION_FAILED);
        }
        User user = userMapper.selectById(application.getUserId());
        if(user == null){
            throw new BusinessException(ErrorCodeConstant.USER_NOT_LOGIN);
        }

        //租客发起租赁申请的房源
        House byHouseId = houseMapper.selectHouseById(houseId);
        //检验申请租赁的房源是否存在
        if(byHouseId == null){
            throw new BusinessException(ErrorCodeConstant.HOUSE_NOT_EXIST);
        }

        //检验房源是否处于可租状态
        if(byHouseId.getStatus()!=0){
            throw new BusinessException(ErrorCodeConstant.HOUSE_STATUS_MISMATCH);
        }

        //检验房源的房东信息与租客申请的房东信息是否一致
        if(!landlordId.equals(byHouseId.getLandlord().getLandlordId())){
            throw new BusinessException(ErrorCodeConstant.LANDLORD_INFO_NOT_MATCH);
        }

        // 设置申请时间和状态
        application.setApplyTime(new Date());
        application.setStatus(0); // 0=房东审核中

        contractMapper.insertInitContract(application);

    }

    /**
     * 房东查看签约申请列表
     */
    @Override
    public List<Contract> getContractApplications(Long landlordId) {
        Landlord landlord = userMapper.selectLandlordByLandlordId(landlordId);
        if (landlord == null) {
            throw new BusinessException(ErrorCodeConstant.USER_AUTH_ERROR);
        }

        return contractMapper.selectContractApplicationsByLandlordId(landlordId);

    }

    /**
     * 房东同意/拒绝合同申请
     * @param contractVerifyRequest 请求参数
     */
    @Override
    public void modifyContractStatus(ContractVerifyRequest contractVerifyRequest) {
        // 1. 验证参数
        if (contractVerifyRequest == null) {
            throw new BusinessException(ErrorCodeConstant.PARAM_VALIDATION_FAILED);
        }
        Long landlordId = contractVerifyRequest.getLandlordId();
        Long contractId = contractVerifyRequest.getContractId();
        Landlord landlord = userMapper.selectLandlordByLandlordId(landlordId);


        if (landlord == null) {
            throw new BusinessException(ErrorCodeConstant.USER_AUTH_ERROR);
        }
        // 2. 查询合同
        Contract contract = contractMapper.selectContractById(contractId);
        if (contract == null) {
            throw new BusinessException(ErrorCodeConstant.CONTRACT_NOT_EXIST);
        }

        // 3. 验证房东是否有权限操作这个合同
        if (!contract.getLandlord().getLandlordId().equals(landlordId)) {
            throw new BusinessException(ErrorCodeConstant.CONTRACT_OPERATION_NOT_AUTHORIZED);
        }

        // 4. 验证合同状态
        if (contract.getSignStatus() != 0) {
            throw new BusinessException(ErrorCodeConstant.CONTRACT_STATUS_MISMATCH);
        }

        // 5. 更新合同状态
        contractMapper.updateContractStatus(contractVerifyRequest);
    }

    /**
     * 租客查看自己的签约申请列表
     */
    @Override
    public List<Contract> getTenantApplications(Long tenantId) {
        User user = userMapper.selectById(tenantId);
        if (user == null) {
            throw new BusinessException(ErrorCodeConstant.USER_NOT_LOGIN);
        }

        return contractMapper.selectContractApplicationsByTenantId(tenantId);
    }

}
